// ReviewsTab.cpp : implementation file
//

#include "stdafx.h"
#include "moretunes.h"
#include "ReviewsTab.h"
#include ".\reviewstab.h"

// Labels names
#define REVIEW_LABEL_NOT_FOUND_NAME						"No review"
#define REVIEW_LABEL_TITLE_NAME							"Review: "
#define REVIEW_LABEL_RATING_NAME						"Rating: "


// CReviewsTab dialog

IMPLEMENT_DYNAMIC(CReviewsTab, CResizablePage)
CReviewsTab::CReviewsTab()
	: CResizablePage(CReviewsTab::IDD)
	, _pos(0)
	, _refresh(FALSE)
	, _currentAlbum(0)
{
}

CReviewsTab::~CReviewsTab()
{
}

void CReviewsTab::DoDataExchange(CDataExchange* pDX)
{
	CResizablePage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT2, _title);
	DDX_Control(pDX, IDC_EDIT3, _rating);
	DDX_Control(pDX, IDC_EDIT1, _review);
}


BEGIN_MESSAGE_MAP(CReviewsTab, CResizablePage)
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedPrevious)
	ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedNext)
ON_WM_PAINT()
ON_WM_CREATE()
END_MESSAGE_MAP()


// CReviewsTab message handlers

BOOL CReviewsTab::setResult(Album* album)
{
	_currentAlbum = album;
	_pos = 0;
	if (_currentAlbum != NULL && _currentAlbum->getReviewRatingsSize() == NULL)
		_currentAlbum = NULL;
	displayAlbum();
	return FALSE;
}

BOOL CReviewsTab::displayAlbum(void)
{
	if (!_refresh)
		return FALSE;
	if (!_currentAlbum) {
		clear();
		return FALSE;
	}
	_title.SetWindowText(REVIEW_LABEL_TITLE_NAME+_currentAlbum->getReviewSummaries(_pos));
	_rating.SetWindowText(REVIEW_LABEL_RATING_NAME+_currentAlbum->getReviewRatings(_pos));
	_review.SetWindowText(_currentAlbum->getReviewReviews(_pos));
	return TRUE;
}

void CReviewsTab::OnBnClickedPrevious()
{
	if (!_currentAlbum)
		return;
	_pos = _pos - 1;
	if (_pos < 0)
		_pos = _currentAlbum->getReviewRatingsSize() - 1;
	displayAlbum();
}

void CReviewsTab::OnBnClickedNext()
{
	if (!_currentAlbum)
		return;
	_pos = (_pos + 1) % _currentAlbum->getReviewRatingsSize();
	displayAlbum();
}

BOOL CReviewsTab::clear(void)
{
	if (!_refresh)
		return FALSE;

	_title.SetWindowText(REVIEW_LABEL_TITLE_NAME);
	_rating.SetWindowText(REVIEW_LABEL_RATING_NAME);
	_review.SetWindowText("");
	return FALSE;
}


void CReviewsTab::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	if (!_refresh){
		_refresh = TRUE;

		CFont font;
		LOGFONT lf;
		memset(&lf, 0, sizeof(LOGFONT));       // zero out structure
		lf.lfHeight = 16;                      // request a 12-pixel-height font
		strcpy(lf.lfFaceName, "Arial");        // request a face name "Arial"
		VERIFY(font.CreateFontIndirect(&lf));  // create the font
		_title.SetFont(&font);

		displayAlbum();
	}
}

int CReviewsTab::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CResizablePage::OnCreate(lpCreateStruct) == -1)
		return -1;
	return 0;
}

